//
//  ViewController.m
//  IconDemo
//
//  Created by LM on 2019/9/29.
//  Copyright © 2019 TJ. All rights reserved.
//

#import "ViewController.h"
#import "ShowDesktopVC.h"
#import "ShowDesktopSearchVC.h"
#import "ShowListVC.h"
#import "ShowAppInfoVC.h"
#import "ShowGoodGoodsVC.h"

@interface ViewController ()<UITableViewDelegate, UITableViewDataSource, UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@property (nonatomic, strong) UIImageView *iconImageView;

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSArray *dataArr;

@property (nonatomic, strong) UIImagePickerController *imagePicker;

@property (nonatomic, strong) ShowDesktopVC *showDesktopVC;
@property (nonatomic, strong) ShowDesktopSearchVC *showDesktopSearchVC;
@property (nonatomic, strong) ShowListVC *showListVC;
@property (nonatomic, strong) ShowGoodGoodsVC *showGoodGoodsVC;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.dataArr = @[@"桌面", @"桌面搜索", @"精品推荐", @"排行榜", @"应用页"];
    
    self.iconImageView = [UIImageView new];
    self.iconImageView.backgroundColor = [UIColor redColor];
    self.iconImageView.userInteractionEnabled = YES;
    [self.view addSubview:self.iconImageView];
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view.mas_top).with.offset(80);
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.width.mas_equalTo(150);
        make.height.mas_equalTo(150);
    }];
    // 添加手势
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
//    btn.backgroundColor = [UIColor blueColor];
    [btn setTitle:@"+" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:50];
    [btn addTarget:self action:@selector(SelPhoto) forControlEvents:UIControlEventTouchUpInside];
    [self.iconImageView addSubview:btn];
    UIEdgeInsets padding = UIEdgeInsetsMake(0, 0, 0, 0);
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.iconImageView).with.insets(padding);
    }];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.iconImageView.mas_bottom).with.offset(50);
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.width.mas_equalTo(200);
        make.bottom.mas_equalTo(self.view.mas_bottom).with.offset(-80);
    }];
    
    // 初始化相册
    self.imagePicker = [[UIImagePickerController alloc] init];
    self.imagePicker.delegate = self;
    self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
       cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.textLabel.text = self.dataArr[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%ld", (long)indexPath.row);
    if (indexPath.row == 0) {
        self.showDesktopVC.photoImage = self.iconImageView.image;
        [self presentViewController:self.showDesktopVC animated:YES completion:nil];
    }else if (indexPath.row == 1) {
        self.showDesktopSearchVC.photoImage = self.iconImageView.image;
        [self presentViewController:self.showDesktopSearchVC animated:YES completion:nil];
    }else if (indexPath.row == 2) {
        self.showGoodGoodsVC.photoImage = self.iconImageView.image;
        [self presentViewController:self.showGoodGoodsVC animated:YES completion:nil];
    }else if (indexPath.row == 3) {
        self.showListVC.photoImage = self.iconImageView.image;
        [self presentViewController:self.showListVC animated:YES completion:nil];
    }else if (indexPath.row == 4) {
        ShowAppInfoVC *vc = [[ShowAppInfoVC alloc] init];
        vc.photoImage = self.iconImageView.image;
        [self presentViewController:vc animated:YES completion:nil];
    }
}

// 选择图片
- (void)SelPhoto
{
    NSLog(@"打开相册");
    [self presentViewController:self.imagePicker animated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey, id> *)info
{
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    self.iconImageView.image = [info objectForKey:UIImagePickerControllerOriginalImage];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:nil];
}

// 懒加载
- (ShowDesktopVC *)showDesktopVC
{
    if (_showDesktopVC == nil) {
        _showDesktopVC = [[ShowDesktopVC alloc] init];
    }
    return _showDesktopVC;
}

- (ShowDesktopSearchVC *)showDesktopSearchVC
{
    if (_showDesktopSearchVC == nil) {
        _showDesktopSearchVC = [[ShowDesktopSearchVC alloc] init];
    }
    return _showDesktopSearchVC;
}

- (ShowGoodGoodsVC *)showGoodGoodsVC
{
    if (_showGoodGoodsVC == nil) {
        _showGoodGoodsVC = [[ShowGoodGoodsVC alloc] init];
    }
    return _showGoodGoodsVC;
}

- (ShowListVC *)showListVC
{
    if (_showListVC == nil) {
        _showListVC = [[ShowListVC alloc] init];
    }
    return _showListVC;
}

@end
